package com.bos.controller;

import com.bos.controller.base.BaseController;
import com.bos.controller.base.imp.BaseControllerImp;
import com.bos.domain.Function;
import com.bos.domain.Role;
import com.bos.domain.User;
import com.bos.service.IRoleService;
import com.bos.utils.BOSUtil;
import com.bos.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author Administrator
 */
@Controller
public class RoleController extends BaseControllerImp<Role> implements BaseController<Role> {

    @Autowired
    private IRoleService roleService;

    @Autowired
    public RoleController(PageBean pageBean){
        super(pageBean);
    }

    @RequestMapping(value = "/addRole",method = RequestMethod.POST)
    public String addRole(Role role,String functionIds){
        roleService.save(role,functionIds);
        return "redirect:/page/admin/role";
    }

    @RequestMapping(value = "/RolePageQuery",method = RequestMethod.GET)
    @ResponseBody
    public String funtionPageQuery(int page ,int rows){
        //将当前页号和页面大小至于session域中，以便刷新时保持不变
        BOSUtil.getSession().setAttribute("RolePage",page);
        BOSUtil.getSession().setAttribute("RoleRows",rows);
        setPageAndRows(page,rows);
        roleService.pageQuery(super.pageBean);
        return java2json(new String[]{"currentPage","pageSize","detachedCriteria","parentFunction",
                "roles","children"});
    }

    @RequestMapping(value = "/listRoles",method = RequestMethod.POST)
    @ResponseBody
    public String listRoles(){
        List<Role> roles = roleService.findAll();
        String json = java2json(roles, new String[]{"functions","users","description","code"});
        return json;
    }

}
